package com.gxa.day2022_07_14.homework.change_system3;

import java.util.Scanner;

import static com.gxa.day2022_07_14.homework.change_system3.Dish.printDishMenu;
import static com.gxa.day2022_07_14.homework.change_system3.OrderSystem3.*;

public class Order {

    static int idx;
    static Scanner scanner =new Scanner(System.in);
    static int isKeepOrder;
    String userName;
    String dishMsg;
    int time;
    String address;
    double sumPrice;
    int orderStatu;

    public Order(String userName,String dishMsg,int time,String address,double sumPrice){
        this.userName= userName;
        this.dishMsg=dishMsg;
        this.time=time;
        this.address =address;
        this.sumPrice =sumPrice;
    }
    public Order(String userName,String dishMsg,int time,String address,double sumPrice,int orderStatu){
        this(userName, dishMsg, time, address, sumPrice);
        this.orderStatu =orderStatu;
    }

    public Order() {

    }




    /**
     * 签收订单
     *
     * @param
     */
    static void signOrder() {
        boolean isSignFind = false;
        System.out.println("***签收订单***");
        printOrder();
        System.out.print("请选择要签收的订单序号：");
        int signOrderId = scanner.nextInt();
//        idx = getDishIndex(signOrderId);
        for (int i = 0; i < orderArray.length; ++i) {
            if (orderArray[i] != null && orderArray[i].orderStatu == 0 && signOrderId == (i + 1)) {
                orderArray[i].orderStatu = 1;
                System.out.println("您的订单签收成功！");
                isSignFind = true;
                printOrder();
            } else if (orderArray[i] != null && orderArray[i].orderStatu == 1 && signOrderId == (i + 1)) {
                System.out.println("你的订单已经签收成功，无法再次签收！");
                isSignFind = true;
            }
        }
        if (!isSignFind) {
            System.out.println("您选择的订单不存在！");
        }

    }

    /**
     * 打印订单
     */
    static void printOrder() {
        System.out.println("序号\t订餐人\t订餐信息\t\t送餐时间\t送餐地址\t\t总金额\t订单状态");
        for (int i = 0; i < orderArray.length; i++) {
            if (orderArray[i] != null && orderArray[i].userName!=null) {
                String statu = (orderArray[i].orderStatu == 0) ? "已预订" : "已完成";
                String time = orderArray[i].time + "点";
                String sumPrice = orderArray[i].sumPrice + "元";
                System.out.println((i + 1) + "\t" + orderArray[i].userName + "\t\t" + orderArray[i].dishMsg + "\t" + time + "\t" + orderArray[i].address + "\t" + sumPrice + "\t" + statu);
            }
        }
    }
    /**
     * 查看餐带
     */
    static void getOrders() {
        System.out.println("***查看餐带***");
        printOrder();
    }
    /**
     * 订餐，创建订单
     */
    static void createOrder() {
        System.out.println("***我要订餐***");

        System.out.print("请输入订餐人姓名：");
        String name = scanner.next();
        printDishMenu();


        System.out.print("请输入菜品序号：");
        idx = scanner.nextInt();

        System.out.print("请输入您需要的份数：");
        int dishChooseNum = scanner.nextInt();

        System.out.print("请输入送餐时间：（送餐时间是：10点到20点 整点送餐）：");
        int time = scanner.nextInt();
        while (time < 10 || time > 20) {
            System.out.println("请输入10--20的整数：");
            time = scanner.nextInt();
        }
        System.out.print("请输入送餐地址：");
        String address = scanner.next();

        System.out.println("订餐成功");
//        idx = getDishIndex(dishChooseId);
        System.out.println("您的订单是：" + dishArray[idx - 1].dishName + " " + dishChooseNum + "份");
        System.out.println("送餐时间：" + time + "点");
        double sumPrice = dishChooseNum * dishArray[idx - 1].dishPrice;
        double deliverFee = sumPrice > 50 ? 0 : 5;

        System.out.println("餐费：" + sumPrice + "元，送餐费：" + deliverFee + "元，总计： " + (sumPrice + deliverFee) + "元");

        for (int i = 0; i < orderArray.length; i++) {//存数据
            if (orderArray[i] == null) {//插入一条返回
                String disMsg = dishArray[i].dishName + " " + dishChooseNum + "份";
                orderArray[i] = new Order(name,disMsg,time,address,sumPrice);
                break;
            }
        }

    }
    /**
     * 删除订单
     *
     * @param
     */
    public static void deleteOrder() {
        boolean isDelFind =false;
        System.out.println("***删除订单***");
        printOrder();
        System.out.print("请输入您要删除的订单序号：");

        int delId = scanner.nextInt();
        for (int i = 0; i < orderArray.length; i++) {//找到要删除的值
            if (orderArray[i] != null && delId == (i + 1) ) {
                isDelFind = true;
                //删除后依次前 移
                for (int j = delId - 1; j < orderArray.length - 1; j++) {//length-1 防止溢出
                    orderArray[j] = orderArray[j+1];
                }
                //最后一位清空
                orderArray[orderArray.length-1] =new Order();
                System.out.println("删除订单成功！");
                printOrder();
            }
        }
        if (!isDelFind) {
            System.out.println("删除失败，该订单不存在！");
        }
    }
}
